package cn.gailvlun.gll.presentation.emotion

import android.databinding.ObservableField
import android.databinding.ObservableFloat
import android.view.View
import cn.gailvlun.gll.R
import cn.gailvlun.gll.chatlib.bean.SystemNotification
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.net.emotion.Emotion
import cn.gailvlun.gll.net.emotion.Heart
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.emotion.core.BaseAdapter
import cn.gailvlun.gll.presentation.emotion.core.BaseViewModel
import cn.gailvlun.gll.presentation.emotion.drawer.EmotionPersonalActivity
import cn.gailvlun.gll.presentation.emotion.filter.EmotionFilterDialog
import cn.gailvlun.gll.presentation.emotion.notification.EmotionNotificationActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.realm.Realm



class EmotionSquareViewModel(
        activity: BaseActivity, adapter: BaseAdapter<Any>?
) : BaseViewModel(activity, adapter) {

    override var mActivity: BaseActivity = activity
    private val mParentView: View
    val textObservable = ObservableField<String>("")
    val filterTextObservable = ObservableField<String>("全部情绪")
    val scaleXObservable = ObservableFloat(1f)
    val scaleYObservable = ObservableFloat(1f)
    var realm: Realm?

    init {
        mParentView = layoutInflater.inflate(R.layout.activity_emotion_square, null)

        realm = Realm.getInstance(ChatDB2.getConfiguration())
        changeNotification()
        initPopWindow(mParentView)

    }

    private var rootViewWidth: Int = 0
    var messageObservable = ObservableField<ArrayList<SystemNotification>>()
        get() {
            if (field.get() == null) {
                field.set(ArrayList())
            }
            return field
        }


    fun jump2Profile(emotion: Emotion) {
        super.baseJump2Profile(emotion.owner)
    }












    fun showReplaceBgWindow() {
        super.baseShowReplaceBgFun(mParentView)
    }


    fun showOperateEmotionWindow(view: View, emotion: Emotion) {
        super.baseShowOperateEmotionWindow(mParentView, emotion)
    }

    fun toEmotionDetail(emotion: Emotion) {
        super.baseToEmotionDetail(emotion)
    }


    fun jump2MyEmotion() {
        EmotionPersonalActivity.openActivity(mActivity)
    }

    fun readMessages() {
        EmotionNotificationActivity.openActivity(mActivity, EmotionNotificationActivity.FromActivityEnum.FROM_SQUARE)

    }

    fun showFunction(anchor: View, emotion: Emotion) {
        super.baseShowFunction(anchor, emotion)
    }

    override fun heartEmotionCallback(heart: Heart) {
        listener?.onHeart(heart)
    }

    interface OnHeartListener {
        fun onHeart(heart: Heart)
    }

    private var listener: OnHeartListener? = null
    fun setOnHeartListener(listener: OnHeartListener) {
        this.listener = listener

    }

    fun onFilter() {
        val newInstance = EmotionFilterDialog(mActivity)
        newInstance.show()
    }

    override fun deleteEmotionCallback(theItem: Emotion) {
        val req = (mActivity as EmotionSquareActivity).req
        req.id = 0
        (mActivity as EmotionSquareActivity).getData(req)

    }
    private fun changeNotification() {

        if (realm != null) {

            realm?.where(SystemNotification::class.java)
                    ?.equalTo("unread", true)
                    ?.beginGroup()
                    ?.equalTo("type", "emotionHeart")
                    ?.or()
                    ?.equalTo("type", "emotionComment")
                    ?.endGroup()
                    ?.isNotNull("emotion")
                    ?.findAll()
                    ?.asFlowable()
                    ?.filter { it.isLoaded }
                    ?.observeOn(AndroidSchedulers.mainThread())
                    ?.subscribe { systemNotifications ->
                        messageObservable.get().clear()
                        messageObservable.get().addAll(systemNotifications)
                        messageObservable.notifyChange()
                    }
        }
    }

    override fun onCleared() {
        super.onCleared()
        realm?.close()
        realm = null
    }


}